package com.wulian.user.rpc;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wulian.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wulian.common.controller.BaseController;
import com.wulian.common.dto.SysUserDto;
import com.wulian.common.dto.UserInfo;
import com.wulian.common.entity.AppResultMoudel;
import com.wulian.common.entity.SysUser;
import com.wulian.common.vo.ResultMoudel;
import com.wulian.common.vo.UserVO;
import com.wulian.user.entity.Post;
import com.wulian.user.mapper.PostMapper;
import com.wulian.user.service.SysUserService;

/**
 * @author scol
 */
@RestController
@RequestMapping(value = "/userRest")
public class UserRest extends BaseController<UserInfo> {

	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private PostMapper postMapper;
	
	@GetMapping("/getUserInfo")
	public UserInfo getUserInfo() {
		UserInfo user = getCurrentUser();
		return user;
	}
	
	/**
	 * 
	* @Title: login 
	* @Description: TODO(用户登录) 
	* @param @param loginUserDto
	* @param @return    设定文件 
	* @return ResultMoudel<UserDto>    返回类型 
	* @throws 
	* @author 马志豪
	* @date 2019年9月19日 下午1:36:26 
	* @version V1.0
	 */
	@PostMapping("sys/login")
	public ResultMoudel login(@RequestBody UserVO userVo){
		//获取当前登录信息
		ResultMoudel login = sysUserService.login(userVo);
		
		return login;
	}

	/**
	 * 功能描述: <br>
	 * 〈〉
	 * @Param: 手机端登录
	 * @Return: 
	 * @Author: caifulin
	 * @Date: 2020/5/6 
	 */
	@SuppressWarnings("rawtypes")
	@PostMapping("sys/loginByApp")
	public AppResultMoudel loginByApp(@RequestBody UserVO userVo){
		//获取当前登录信息
		AppResultMoudel login = sysUserService.loginByApp(userVo);

		return login;
	}
	
	/**
	 * 
	* @Title: updatePassword
	* @Description: (App端修改密码)
	* @author 马志豪
	* @param @param userDto
	* @param @return    设定文件
	* @return AppResultMoudel    返回类型
	* @throws
	 */
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@PostMapping("/updatePassword")
    public AppResultMoudel updatePassword(@RequestBody SysUserDto userDto) {
    	AppResultMoudel appResultMoudel = new AppResultMoudel();
        UserInfo user = getCurrentUser();
        if (user==null||StringUtils.isBlank(userDto.getOldPassword())||StringUtils.isBlank(userDto.getNewpassword1())){
            return appResultMoudel.error(null, "参数不全");
        }
        userDto.setUserId(user.getSysUser().getUserId());
        ResultMoudel resultMoudel= sysUserService.updatePassword(userDto);
        
        if(null != resultMoudel){
        	if("success".equals(resultMoudel.getResultFlag())){
        		appResultMoudel.success(null, (String)resultMoudel.getBody());
        	}else if("error".equals(resultMoudel.getResultFlag())){
        		appResultMoudel.error(null, (String)resultMoudel.getBody());
        	}
        }
        return appResultMoudel;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping("/getPersonalDetails")
	public AppResultMoudel getPersonalDetails() {
    	HashMap<String,Object> hashMap = new HashMap<String,Object>();
		UserInfo user = getCurrentUser();
		SysUser sysUser = user.getSysUser();
		
		List<Integer> postIds = sysUser.getPostIds();
		
		String post = "";
		if(null != postIds && postIds.size() > 0){
			List<Post> posts = postMapper.selectBatchIds(postIds);
			post = posts.stream().map(p -> p.getName()).collect(Collectors.joining(","));
		}
		
		hashMap.put("name", sysUser.getName());
		hashMap.put("company", sysUser.getCompanyName());
		hashMap.put("post", post);
		hashMap.put("phone", sysUser.getPhone());
		hashMap.put("email", sysUser.getEmail());
		return new AppResultMoudel().success(hashMap, "查询个人信息成功");
	}

	/**
	 * 功能描述: <br>
	 * 〈根据用户名查询用户信息〉
	 * @Param:
	 * @Return:
	 * @Author: caifulin
	 * @Date: 2020/11/19
	 */
	@GetMapping("/getUserIdListByUserName")
	public List<SysUser> getUserIdListByUserName(@RequestParam("userName") String userName,@RequestParam("companyId") Integer companyId){
		EntityWrapper<SysUser> sysUserEntityWrapper = new EntityWrapper();
		sysUserEntityWrapper.like("name",userName);
		sysUserEntityWrapper.and().eq("company_id",companyId);
		List<SysUser> sysUsers = sysUserService.selectList(sysUserEntityWrapper);
		return sysUsers;
	}
}
